package com.brtbeacon.demo.lot.state;

import android.view.View;

import androidx.fragment.app.Fragment;

import com.brtbeacon.demo.lot.LotMapActivity;
import com.brtbeacon.demo.lot.R;
import com.brtbeacon.demo.lot.entity.ParkingConstant;
import com.brtbeacon.demo.lot.fragment.ParkingTargetControlFragment;
import com.brtbeacon.map.map3d.BRTMapView;
import com.brtbeacon.map.map3d.entity.BRTPoi;
import com.brtbeacon.map.map3d.entity.BRTPoint;
import com.brtbeacon.map.map3d.route.BRTMapRouteManager;
import com.brtbeacon.map.map3d.route.BRTRouteResult;
import com.brtbeacon.map.map3d.utils.BRTConvert;
import com.brtbeacon.mapdata.BRTLocalPoint;

import java.util.List;

/**
 * 编辑导航终点状态
 */

public class ParkingTargetState extends ParkingState implements View.OnClickListener {

    private ParkingTargetControlFragment parkingTargetControlFragment;

    @Override
    public void enter(LotMapActivity activity) {
        super.enter(activity);
        this.activity.setTopFragment(null);
        this.activity.setBottomFragment(getBottomFragment());
        this.activity.clearRouteResult();
        this.activity.getMapView().showRoutePassed(null);
    }

    @Override
    public void exit() {
        if (this.activity != null) {
            this.activity.setTopFragment(null);
            this.activity.setBottomFragment(null);
        }
        super.exit();
    }

    private ParkingTargetControlFragment getBottomFragment() {
        if (parkingTargetControlFragment == null) {
            parkingTargetControlFragment = ParkingTargetControlFragment.newInstance();
            parkingTargetControlFragment.setState(this);
        }
        return parkingTargetControlFragment;
    }

    @Override
    public void onClickAtPoint(BRTMapView mapView, BRTPoint point) {
        super.onClickAtPoint(mapView, point);

    }
    
    @Override
    public void onPoiSelected(BRTMapView brtMapView, List<BRTPoi> list) {
        for (BRTPoi poi : list) {
            if (ParkingConstant.isParkingPoi(poi.getCategoryId())) {
                this.activity.setParkingEndPoi(poi);
                updateTargetPoiUI();
                break;
            }
        }
    }

    @Override
    public void onFragmentViewCreated(Fragment fragment, View view) {
        super.onFragmentViewCreated(fragment, view);
        parkingTargetControlFragment.btnOK.setOnClickListener(this);
        updateTargetPoiUI();
    }

    private void updateTargetPoiUI() {
        if (parkingTargetControlFragment.isVisible()) {
            BRTPoi poi = activity.getParkingPoi();
            this.parkingTargetControlFragment.tvInfo.setText(poi!=null?poi.getName():"");
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ok: {
                //  设置停车点；
                if (activity.getLastLocation() == null) {
                    activity.showToast("未获取到你的位置信息！");
                    return;
                }

                if (activity.getEndPoint() == null) {
                    activity.showToast("请选择停车位！");
                    return;
                }

                activity.requestRoute();
                break;
            }
        }
    }

    @Override
    public void didSolveRouteWithResult(BRTMapRouteManager routeManager, BRTRouteResult routeResult) {
        super.didSolveRouteWithResult(routeManager, routeResult);
        activity.setState(ParkingState.PARKING_STATE_ROUTE);
    }
}
